<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html"; charset="utf-8"/>
  	<link type="text/css" href="css/makeRelations.css" rel="stylesheet" />
	<title>For the Snark was a Boojum, you see.</title>
</head>
<body>
<? 
if($_POST["subjectKind"]){
	include_once(dirname(__FILE__).'/config.php');
	$store = ARC2::getStore($arc_config);

	$timestamp = time();

	$subjectsId = unserialize(stripslashes($_POST["subjectsId"]));

	if($_POST["objectsId"]){
		$objectsId = unserialize(stripslashes($_POST["objectsId"]));
	}
	if($_POST["relationId"]){
		$relationId = $_POST["relationId"];
	}
	if($_POST["relationLabel"]){
		$r = ereg("(.*):(.*)", $_POST["relationLabel"], $regs);
		if(!$r) exit("nouvelle relation mal nommée");
		$vocab = rawurlencode(strtolower($regs[1]));
		$relation = rawurlencode(strtolower($regs[2]));
		$q = "PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
			SELECT * WHERE {
				?vocab a vocabs:Vocabulary .
				?vocab rdfs:label \"$vocab\" .
			}";
		$rs = $store->query($q);
		if(!$store->getErrors()){
			$rows = $rs['result']['rows'];
			switch(sizeof($rows)){
			case 1:
				$vocabId = $rows[0]["vocab"];
				break;
			case 0:
				$timestamp += 1;
				$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
PREFIX user: <http://desanti.org/users/>
PREFIX traces: <http://desanti.org/schemas/traces#>

INSERT INTO <http://desanti.org/> {
	<http://desanti.org/schemas/$vocab>	a		vocabs:Vocabulary .
	<http://desanti.org/schemas/$vocab>	rdfs:label	"$vocab" .
	<http://desanti.org/schemas/$vocab>	vocabs:hasUser	user:{$_POST['user']} .

	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	a	vocabs:AddVocabulary .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:hasDoer user:{$_POST['user']} .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:timestamp "$timestamp" .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:withVocabulary <http://desanti.org/schemas/$vocab> .

}
EOT;
				//print_r($q);
				$rs = $store->query($q);
				if($store->getErrors()) exit($store->getErrors());

				$vocabId = "http://desanti.org/schemas/$vocab";
				break;
			default:
				echo "<pre>"; print_r($rows); echo "</pre>";	
				exit("plusieurs vocabulaires avec le même nom ??!");
				break;
			}
			$timestamp += 1;
			$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
PREFIX user: <http://desanti.org/users/>
PREFIX traces: <http://desanti.org/schemas/traces#>

INSERT INTO <http://desanti.org/> {
	<$vocabId#$relation>		a			rdf:Property .
	<$vocabId#$relation>		rdfs:label		"$relation" .
	<$vocabId>			vocabs:hasRelation	<$vocabId#$relation> .

	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	a	vocabs:AddRelation .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:hasDoer user:{$_POST['user']} .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:timestamp "$timestamp" .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:withVocabulary <$vocabId> .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:withRelation <$vocabId#$relation> .
}
EOT;
			//print_r($q);
			$rs = $store->query($q);
			if($store->getErrors()) exit($store->getErrors());
		}else{
			echo "<pre>"; print_r($store->getErrors()); echo "</pre>";
			exit("erreur 1");	
		}
		$relationId = "{$vocabId}#{$relation}";
	}
	if($_POST["newTerms"]){
		$newTerms = unserialize(stripslashes($_POST["newTerms"]));
		for($i=0; $i<sizeof($newTerms); $i++){
			$r = ereg("(.*):(.*)", rawurldecode($newTerms[$i]), $regs);
			if(!$r) exit("nouveau terme mal nommé");
			$class = rawurlencode(ucfirst(strtolower($regs[1])));
			$term = rawurlencode(strtolower($regs[2]));
			$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
SELECT * WHERE {
	?class a vocabs:Class .
	?class rdfs:label "$class" .
}
EOT;
			$rs = $store->query($q);
			if(!$store->getErrors()){
				$rows = $rs['result']['rows'];
				switch(sizeof($rows)){
				case 1:
					$classId = $rows[0]["class"];
					break;
				case 0:
					$timestamp += 1;
					$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
PREFIX user: <http://desanti.org/users/>
PREFIX traces: <http://desanti.org/schemas/traces#>

INSERT INTO <http://desanti.org/> {
	<http://desanti.org/classes/$class>	a		vocabs:Class .
	<http://desanti.org/classes/$class>	rdfs:label	"$class" .
	<http://desanti.org/classes/$class>	vocabs:hasUser	user:{$_POST['user']} .

	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	a	vocabs:AddClass .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:hasDoer user:{$_POST['user']} .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:timestamp "$timestamp" .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:withClass <http://desanti.org/classes/$class> .

}
EOT;
					//print_r($q);
					$rs = $store->query($q);
					if($store->getErrors()) exit($store->getErrors());

					$classId = "http://desanti.org/classes/$class";
					break;
				default:
					echo "<pre>"; print_r($rows); echo "</pre>";	
					exit("plusieures classes avec le même nom ??!");
					break;
				}
				$timestamp += 1;
				$termId = 'http://desanti.org/things/'.strtolower($class)."#$term";
				$q = <<<EOT
PREFIX vocabs: <http://desanti.org/schemas/vocabularies#>
PREFIX user: <http://desanti.org/users/>
PREFIX traces: <http://desanti.org/schemas/traces#>

INSERT INTO <http://desanti.org/> {
	<$termId>		a			<$classId> .
	<$termId>		rdfs:label		"$term" .

	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	a	vocabs:AddTerm .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:hasDoer user:{$_POST['user']} .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:timestamp "$timestamp" .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:withClass <$classId> .
	<http://desanti.org/users/{$_POST['user']}/actions/$timestamp>	traces:withTerm <$termId> .
}
EOT;
				//print_r($q);
				$rs = $store->query($q);
				if($store->getErrors()) exit($store->getErrors());
			}else{
				echo "<pre>"; print_r($store->getErrors()); echo "</pre>";
				exit("erreur 1");	
			}
			$objectsId[] = "$termId";
		}
	}
	
	for($i=0; $i<sizeof($subjectsId); $i++){
		for($j=0; $j<sizeof($objectsId); $j++){
			$q = <<<EOT
INSERT INTO <http://desanti.org/> {
	<{$subjectsId[$i]}> <$relationId> <{$objectsId[$j]}> .
}
EOT;
			//print_r($q);
			$rs = $store->query($q);
			if($store->getErrors()) exit($store->getErrors());
		}
	}
}

if($_POST["subject"]){ 
	$subjects = json_decode(stripslashes($_POST["subject"]));
	$objects = json_decode(stripslashes($_POST["object"]));

	for($i = 0; $i < sizeof($subjects); $i++){
		$subjectsId[] = $subjects[$i]->id;
	}
	for($i = 0; $i < sizeof($objects); $i++){
		if($objects[$i]->type == 'term'){
			if($objects[$i]->id == ""){
				$newTerms[] = rawurlencode($objects[$i]->label);
			}else
			{
				$objectsId[] = $objects[$i]->id;
			}
		}else{
			$objectsId[] = $objects[$i]-> id;
		}
	}
?>
	<div id="controls">
		<form id="makeRelations" method="post" action="<? echo $_SERVER['PHP_SELF'];  ?>">
			<input type="radio" name="subjectKind" value="one">unique</input>
			<input type="radio" name="subjectKind" value="many" checked="true">multiple</input>
			<? if(isset($subjectsId)){ ?>
				<input type="hidden" name="subjectsId" value="<? echo htmlentities(serialize(($subjectsId))); ?>"/>
			<? } ?>

			<? if(isset($objectsId)){ ?>
				<input type="hidden" name="objectsId" value="<? echo htmlentities(serialize(($objectsId))); ?>"/>
			<? } ?>

			<? if(isset($newTerms)){ ?>
				<input type="hidden" name="newTerms" value="<? echo htmlentities(serialize(($newTerms))); ?>"/>
			<? } ?>

			<? if($_POST["relationId"] == ""){ ?>
				<input type="hidden" name="relationLabel" value="<? echo $_POST["relationLabel"]; ?>"/>
			<? }else{ ?>
				<input type="hidden" name="relationId" value="<? echo $_POST["relationId"]; ?>"/>
			<? } ?>
			<input type="hidden" name="user" value="<? echo $_POST['user']; ?>"/>
			<input type="submit" value="sauver"/>
		</form>
	</div>
	<div id="subjects">
		<? 
		for($i = 0; $i < sizeof($subjects); $i++){
			echo '<div class="subject">';	
			echo '<img src="'.$subjects[$i]->thumb.'"/>';
			echo '</div>';
		} 
		?>
	</div>
	<div id="relation"><? echo($_POST["relationLabel"]); ?></div>
	<div id="objects">
		<? 
		for($i = 0; $i < sizeof($objects); $i++){
			if($objects[$i]->type == 'term'){
				echo '<div class="object">';	
				echo '<div class="term">'.$objects[$i]->label.'</div>';	
				echo '</div>';
			}else{
				echo '<div class="object">';	
				echo '<img src="'.$objects[$i]->thumb.'"/>';
				echo '</div>';
			}
		} 
		?>
	</div>
<? } ?>
</body>
</html>
